﻿using System;
using System.Windows;
using System.Windows.Controls;
using SomethingSpacialClassLib.Web;
using SomethingSpacial.ViewModel;
using SomethingSpacial.Model;

namespace SomethingSpacial.Views.Admin
{
    public partial class SponsorList : Page
    {

        SomethingSpacialContext domainContext = new SomethingSpacialContext();

        public SponsorList()
        {
            InitializeComponent();
            Loaded +=new RoutedEventHandler(SponsorList_Loaded);    
            
        }

        #region "Page Events"

        void SponsorList_Loaded(object sender, RoutedEventArgs e)
        {
            ((SponsorsViewModel)this.DataContext).loadSponsors();
        }


        private void buttonUpdate_Click(object sender, RoutedEventArgs e)
        {
            editDataForm.CommitEdit();
 
            if (editDataForm.CurrentItem != null)
            {
               // SponsorModel sm = editDataForm.CurrentItem as SponsorModel;
                ((SponsorsViewModel)this.DataContext).UpdateSponsor();
            }
        }

        private void buttonAddNew_Click(object sender, RoutedEventArgs e)
        {

            buttonUpdate.Visibility = Visibility.Collapsed;
            buttonDelete.Visibility = Visibility.Collapsed;
            buttonSaveNew.Visibility = Visibility.Visible;

            Sponsor NewSponsor = new Sponsor();
     
            NewSponsor.SPN_Id = 0;
            NewSponsor.SPN_Name = "New Sponsor";
            NewSponsor.SPN_IsActive = true;
            NewSponsor.SPN_Description = "Sponsor Description";
            //NewSponsor.LogoImage = new ushort("Microsoft.jpg");
            NewSponsor.SPN_URL = "http://www.Microsoft.com";

            editDataForm.CurrentItem = NewSponsor;
            editDataForm.BeginEdit();


        }


     

        private void buttonSaveNew_Click(object sender, RoutedEventArgs e)
        {
            Button buttonCurrent = (Button)sender;
            String ButtonText = buttonCurrent.Content.ToString();
            editDataForm.CommitEdit();
            ((SponsorsViewModel)this.DataContext).addSponsor((Sponsor)editDataForm.CurrentItem);
            editDataForm.CurrentItem = null;

        }

        private void gridSponsors_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            buttonSaveNew.Visibility = Visibility.Collapsed;
            buttonUpdate.Visibility = Visibility.Visible;
            buttonDelete.Visibility = Visibility.Visible;

            editDataForm.CurrentItem = gridSponsors.SelectedItem;
            editDataForm.BeginEdit();
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            editDataForm.CommitEdit();
            ((SponsorsViewModel)this.DataContext).DeleteSponsor((Sponsor)editDataForm.CurrentItem);
        }


        #endregion


    }
}
